$(document).ready(function() {
  require(["inputdrop", "asset", "tree"], function(inputdrop, asset, tree) {
    var list_url = "user/queryUserList";
    var list_col = [
      { name: "", text: "", hideSearch: "hide", width: 3 },
      {
        text: "角色名称",
        name: "roleName",
        align: "left",
        width: 15,
        tip:true,
        searchRender: function(el) {
          el.append(
            '<input type="text" class="form-control input-sm" search-data="userName">'
          );
        }
      },
      { text: "相关用户", name: "userName", align: "left", width: 15 },
      { text: "功能模块", name: "funcName", align: "left", width: 67 }
    ];
    var index_oper = [
      { icon: "rh-icon rh-edit", text: "编辑", aclick: edit_template_init },
      {
        icon: "rh-icon rh-delete",
        text: "删除",
        aclick: delete_list_init,
        isShow: function(rowData) {
          if (rowData.system) {
            return false;
          } else {
            return true;
          }
        }
      }
    ];

    event_init();

    user_list({ paramObj: null, isLoad: true, maskObj: "body" });

    index_search_div_remove_click(user_list, {
      paramObj: null,
      isLoad: true,
      maskObj: "body"
    });

    function event_init() {
      $("input[placeholder]").placeholder();      
      $("#add_btn").click(function() {
        edit_template_init();
      });

      $("#sec_setting_btn").click(function() {
        sec_setting_template_init();
      });
      
      $("#searchIcon").click(function() {
        var key=$("#searchKey").val()
        user_list({
          paramObj: {roleName:key},
          isLoad: true,
          maskObj: "body"
        });
      });
      $("#searchKey").keydown(function(event) {
        if (event.keyCode == 13 ) {
          $("#searchIcon").trigger("click");
        }
      });
      asset.user_init();
    }

    function user_list(option) {
      g_grid.render($("#table_div"), {
        header: list_col,
        oper: index_oper,
        operWidth: "130px",
        url: "api/role/func/list",
        type: "xd",
        dataKey: "list",
        hasBorder:false,
        hideSearch : true,
        paramObj: option.paramObj,
        isLoad: option.isLoad,
        maskObj: option.maskObj,
        dbClick: detail_template_init,
        // dbIndex: 1,
        autoHeight : true,
        showCount : true,   
        allowCheckBox:false
      });
    }

    function delete_list_init(rowData) {
      if(rowData.userName!==""){
        g_dialog.operateAlert(null, "当前角色存在关联用户，无法删除","error");
        return false
      }
      g_dialog.operateConfirm("确认删除此角色？", {
        saveclick: function() {
          xd_ajax_post({
            url: "api/role/delete",
            paramObj: {id:rowData.id},
            successCallBack: function(data) {
              user_list({ maskObj: $("#table_div") });
              g_dialog.operateAlert(null, "操作成功！");
            }
          });
        }
      });
    }

    function edit_template_init(rowData) {
      var title = rowData ? "角色修改" : "角色添加";
      $.ajax({
        type: "GET",
        url: "module/sys_config/sys_manage_tpl.html",
        success: function(data) {
          g_dialog.dialog($(data).find("[id=add_role_template]"), {
            width: "560px",
            init: init,
            initAfter: initAfter,
            title: title,
            saveclick: save_click
          });
        }
      });

      function init(el) {
        var roleId={};
          rowData?roleId.id=rowData.id:"";
        xd_ajax_get({
          url: "api/role/func/choiselist",
          paramObj: roleId,
          isLoad:true,
          successCallBack: function(data) {
            tree.render($("#accordion"), {
              pId: "parentId",
              label: "label",
              id: "id",
              chk: true,
              zNodes: data,
              zTreeOnClick: function(event, treeId, treeNode) {}
            });
          }
        });
       
        if (rowData) {
          el.umDataBind("render", rowData);
        }
      }

      function initAfter(el) {
      }

      function save_click(el, saveObj) {
        var roleFunc = tree.getCheckedNodes($("#accordion"))
        if(roleFunc.length<1){
          g_dialog.operateAlert(null, "至少选择一个功能模块！", 'error');
          return false
        }
        var aRoleIds=[]
        roleFunc.forEach(function(item) {
          var roleId={}
          roleId.funcId=item.id
          aRoleIds.push(roleId)
        }, this);
        saveObj.funcMenus=aRoleIds
        if (validateForm($("#query_form"))) {
          xd_ajax_post({
            url: "api/role/save",
            paramObj: saveObj,
            maskObj: el,
            successCallBack: function(data) {
              g_dialog.operateAlert();
              g_dialog.hide(el);
              g_grid.refresh($("#table_div"));
            }
          });
        }
      }
    }

    function detail_template_init(rowData) {}

    function role_batch_operate() {
      var row_data = g_grid.getData($("#table_div"), { chk: true });
      var user = [];
      row_data.forEach(function(item) {
        var idObj = {};
        idObj.id = item.id;
        user.push(idObj);
      }, this);
      xd_ajax_post({
        url: "api/public/role/delete",
        paramObj: user,
        successCallBack: function(res) {
          console.log(res);
        }
      });
    }

    $("#batch_del").click(function() {
      role_batch_operate();
    });
  });
});
